﻿@model DHQGHN.PM0.Models.Admin.MenuInRoleModel
@{

    var selectList = ViewBag.lstMenu;
    var selectListCon = ViewBag.lstMenuCon;
    var columnsMenu = 2;
    var rowsMenu = (int)Math.Round((double)selectList.Items.Count / (double)columnsMenu + 0.5);
    var rowChildMenu = (int)Math.Round((double)selectListCon.Items.Count / (double)columnsMenu + 0.5);
    int index = 0;
}
@using (
    Html.BeginForm(
    actionName: "actionCreateOrUpdateMenuInRole", controllerName: "MenuInRole", routeValues: new { }, method: FormMethod.Post
        , htmlAttributes: new { @id = "menu-in-role-form", @class = "form-inline", @role = "form", @style = "width: 100%;" }))
{
    <div class="modal-body min-width">
        <div class="form-menu">
            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.MenuInRoleID)
            
            <div>
                @Html.LabelFor(m => m.AppID, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.AppID, new SelectList(ViewBag.lstApp, "value", "text", 0), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 80% !important", @disabled = true })
                <br />
                @Html.ValidationMessageFor(m => m.AppID, null, new { @class = "Validation_Error", @style = "margin-left:120px" })
            </div>

            <div>
                @Html.LabelFor(m => m.RoleID, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.RoleID, new SelectList(ViewBag.lstRole, "value", "text", @Model.RoleID), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 80% !important", @disabled = true })
                @Html.ValidationMessageFor(m => m.RoleID, null, new { @class = "Validation_Error", @style = "margin-left:120px" })
            </div>            
            @Html.Label("Chức năng cha", new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
            <div id="listMenu" style="resize: none; overflow: auto; max-height: 180px; margin-bottom: 20px">
                @if (!(selectList.Items.Count == 0 || selectList == null))
                {
                    <table class="tbl-list" style="margin-left: 115px; width: 80%;">

                        @for (int i = 0; i < rowsMenu; i++)
                        {
                            <tr>
                                @for (int j = 0; j < columnsMenu; j++)
                            {
                                index = columnsMenu * i + j;
                                if (index >= selectList.Items.Count) { break; }
                                <td style="width:30%" class="td-onclick">
                                    <div id="checkboxMenuChecked">
                                        <input type="checkbox" name="@selectList.Items[index].Value" />&nbsp;&nbsp;&nbsp;@selectList.Items[index].Text
                                    </div>
                                </td>

                            }
                            </tr>
                        }
                    </table>
                    
                }
                else if (ViewBag.AppID > 0)
                {
                    <span class="field-validation-error" style="margin-left:120px"> Ứng dụng không có hoặc đã thêm hết chức năng </span>
                }
            </div>
            @Html.Label("Chức năng con", new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
            <div id="listMenu" style="resize: none; overflow: auto; max-height: 180px; margin-bottom: 20px">
                @if (!(selectListCon.Items.Count == 0 || selectListCon == null))
                {
                    <table class="tbl-list" style="margin-left: 115px; width: 80%;">

                        @for (int i = 0; i < rowChildMenu; i++)
                        {
                            <tr>
                                @for (int j = 0; j < columnsMenu; j++)
                            {
                                index = columnsMenu * i + j;
                                if (index >= selectListCon.Items.Count) { break; }
                                    <td style="width:30%" class="td-onclick">
                                        <div id="checkboxMenuChecked">
                                            <input type="checkbox" name="@selectListCon.Items[index].Value" />&nbsp;&nbsp;&nbsp;@selectListCon.Items[index].Text
                                        </div>
                                    </td>

                            }
                            </tr>
                        }
                    </table>
                    
                }
                else if (ViewBag.AppID > 0)
                {
                    <span class="field-validation-error" style="margin-left:120px"> Ứng dụng không có hoặc đã thêm hết chức năng </span>
                }
            </div>
            <div>
                @Html.LabelFor(m => m.Descriptions, new { @class = "labels", @for = "descriptions", style = "width:100px;" }) <span> &nbsp; </span>
                @Html.TextAreaFor(m => m.Descriptions, new { @class = "TextBoxContent", style = "width:80%; height: 40px; vertical-align:top; resize:none", @placeholder = "", @cols = 80, @rows = 10 })
                <br />@Html.ValidationMessageFor(m => m.Descriptions, null, new { @class = "Validation_Error", @style = "margin-left:120px" })
            </div>
        </div>
    </div>
}

<div id="overlay" class="web_dialog_overlay"></div>
@Scripts.Render("~/bundles/jqueryval")
<script>
        @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script>

    var $menuInRoleForm = $("#menu-in-role-form");
    function save() {
        var appid = $('#AppID').val().trim();
        var roleid = $('#RoleID').val().trim();
        var lstMenuID = getListMenuID();
        var result = false;
        $('#RoleID').prop("disabled", false);

        if (appid > -1 && roleid > -1 && lstMenuID.length != 0) {
            if ($menuInRoleForm.valid()) {
                $.ajax({
                    contentType: "application/json",
                    type: "POST",
                    url: $menuInRoleForm.attr('action'),
                    data: JSON.stringify({ AppID: appid, RoleID: roleid, lstMenuID: lstMenuID, Descriptions: $('#Descriptions').val() }),
                    dataType: 'json',//chú ý là form đặc biệt thì phải điền trường này
                    async: false,
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    success: function (response) {
                        if (response != null && response.status == 200) {
                            window.notice(response.result, window.notice_success);
                            OnCriteria(crrPageIndex);
                            result = true;
                        } else {
                            window.notice(response.result, window.notice_error);
                        }
                    }
                });
            }
        }
        $('#RoleID').prop("disabled", true);
        return result;
    }

    $('#AppID').change(function () {
        var appid = $('#AppID').val().trim();
        var roleid = $('#RoleID').val().trim();
        var $catlist = $('#listMenu');

        $.ajax({
            dataType: "html",
            type: "POST",
            url: '/MenuInRole/reloadParentMenu',
            contentType: "application/json",
            data: JSON.stringify({ appId: appid, roleID: roleid }),
            success: function (response) {
                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.empty();
                    $catlist.html(response);
                }
                
            }, error: function () {
                console.log("Hihi");
            }
        });
    });

    $('.td-onclick').click(function () {
        $(this).find(':checkbox').click();
    });

    $(":checkbox").click(function (e) {
        e.stopPropagation();
    });

    function getListMenuID() {
        var values = [];
        $('#checkboxMenuChecked input:checked').each(function () {
            values.push(this.name);
        });
        return values;
    }

</script>
